package cn.itcast.nio.c1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

public class TestFileChannelTransferTo {
    public static void main(String[] args)  {
        try (
                FileChannel from=new FileInputStream("C:\\Users\\dell\\IdeaProjects\\Netty\\netty-demo\\data.txt").getChannel();
                FileChannel to=new FileOutputStream("C:\\Users\\dell\\IdeaProjects\\Netty\\netty-demo\\to.txt").getChannel();
                ){
            //文件的大小
            long size= from.size();
            //left表示还剩余多少字节
            for(long left=size;left>0;){
                System.out.println("position:"+(size-left)+"  left:"+left);
                left-=from.transferTo((size-left),left,to);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
